<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="70">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="学年" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="截止时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.end_date }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.status | statusFilter }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作" width="250">
        <template slot-scope="{row, $index}">
          <div>
            <router-link :to="{path:'./status',query: {row}}">
              <el-button type="text" size="mini" round>查看进度</el-button>
            </router-link>
            <router-link :to="{path:'./publish',query: {row}}">
              <el-button type="text" size="mini" round>编辑</el-button>
            </router-link>
            <el-button type="text" size="mini" round @click="handleDelete(row, $index)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import * as task from '@/api/task'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = ['未上传学生基本信息表', '未上传班级表', '已发布']
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      task.list().then(response => {
        this.list = response.data
        this.listLoading = false
        console.log(this.list)
      })
    },
    handleModifyStatus(row, status) {
      row.status = status
      console.log(row)
      task.update(row).then(() => {
        this.$message({
          message: '操作Success',
          type: 'success'
        })
      }).catch(e => {
        this.listLoading = false
      })
    },
    handleDelete(row, index) {
      this.$confirm('此操作将永久删除该, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        task.del({ 'id': row.id }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        this.list.splice(index, 1)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
